﻿using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using Web7.Extensions;
using Web7.Core.Apps;
using Web7.Mvc.ViewEngines;
using Web7.Core.ViewResolvers;

namespace Web7.Core.ActionFilters.Result
{

    public class ViewEnginesResultFilter : IResultFilter, IExceptionFilter
    {
        private readonly IAppRegistry _apps;
        private readonly IViewResolverRegistry _viewResolverRegistry;

        public ViewEnginesResultFilter(IAppRegistry apps, IViewResolverRegistry viewResolvers)
        {
            this._apps = apps;
            this._viewResolverRegistry = viewResolvers;
        }

        #region IResultFilter Members

        public void OnResultExecuted(ResultExecutedContext filterContext)
        {
        }

        public void OnResultExecuting(ResultExecutingContext filterContext)
        {
            SetupSkinViewEngines(filterContext.RequestContext, filterContext.Controller.ViewData, filterContext.Result as ViewResultBase);
        }

        #endregion

        #region IExceptionFilter Members

        public void OnException(ExceptionContext filterContext)
        {
            SetupSkinViewEngines(filterContext.RequestContext, filterContext.Controller.ViewData, filterContext.Result as ViewResultBase);
        }

        #endregion

        #region Public Methods

        public void SetupSkinViewEngines(RequestContext requestContext, ViewDataDictionary viewData, ViewResultBase result)
        {
            if (result == null) return;

            //当前模块名称
            string currentAppName = requestContext.RouteData.DataTokens["AppName"] as string;
            if (currentAppName.IsNullOrWhiteSpace()) return;

            //当前模块
            IApp currentApp = _apps.GetApps<IApp>().FirstOrDefault(m => m.AppName == currentAppName);
            if (currentApp == null) return;

            //HttpRequestBase request = requestContext.HttpContext.Request;
            //string queryStringSkinName = request.QueryString["skin"];
            ////TODO::
            //string cookieSkinName = string.Empty; ;// request.Cookies.GetSkinName();
            //string skin = null;

            //if (!string.IsNullOrEmpty(queryStringSkinName))
            //    skin = queryStringSkinName;
            //else if (!string.IsNullOrEmpty(cookieSkinName))
            //    skin = cookieSkinName;

            
            ////如果QueryString和Cookie都没有包含Skin值            
            //if (skin.IsNullOrWhiteSpace())
            //{
            //    skin = viewData["Skin"] as string;
            //}
            //if (skin.IsNullOrWhiteSpace())
            //{
            //    skin = currentModule.Settings["Skin"];
            //}

            string viewRoot = currentApp.AppName;

            //GenerateViewEngines方法
            //通过ViewEngines.Engines生成新的IEnumerable<IWeb7ViewEngine>
            //Commented By  :HaiJun
            //Commented Date:2012-4-5
            //Description   : 这儿没有必要进行优化，这儿需要根据AppName与Request的信息取得相关的视图引擎。
            //                如不同App下的手机版，网页版等信息。如果不是性能问题特别突出，就不需要改变。
            //                这儿是将视图引擎绑定到ViewResult中的，不是全局的。每个请求根据需要绑定不同的视图引擎。
            IEnumerable<IWeb7ViewEngine> viewEngines = _viewResolverRegistry.GenerateViewEngines(new ViewResolverContext(requestContext, viewRoot));

            //如果ActionResult是ViewResultBase值，则result参数不会为null
            //这里将把result的ViewEngineCollection设置为GenerateViewEngines方法刚才生成的视图引擎集合。
            //也就是说，在ViewResult执行之前，默认的视图引擎就被替换了
            result.ViewEngineCollection = new ViewEngineCollection(viewEngines.Cast<IViewEngine>().ToList()); ;

            viewData["SkinViewEngines"] = viewEngines;
        }

        #endregion

    }
}
